#
# (C) Copyright 2000-2006
# Wolfgang Denk, DENX Software Engineering, wd@denx.de.
#
# See file CREDITS for list of people who contributed to this
# project.
#
# This program is free software; you can redistribute it and/or
# modify it under the terms of the GNU General Public License as
# published by the Free Software Foundation; either version 2 of
# the License, or (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program; if not, write to the Free Software
# Foundation, Inc., 59 Temple Place, Suite 330, Boston,
# MA 02111-1307 USA
#

VERSION = 1
PATCHLEVEL = 1
SUBLEVEL = 4
EXTRAVERSION =
U_BOOT_VERSION = $(VERSION).$(PATCHLEVEL).$(SUBLEVEL)$(EXTRAVERSION)
VERSION_FILE = include/version_autogenerated.h

HOSTARCH := $(shell uname -m | \
	sed -e s/i.86/i386/ \
	    -e s/sun4u/sparc64/ \
	    -e s/arm.*/arm/ \
	    -e s/sa110/arm/ \
	    -e s/powerpc/ppc/ \
	    -e s/macppc/ppc/)

HOSTOS := $(shell uname -s | tr '[:upper:]' '[:lower:]' | \
	    sed -e 's/\(cygwin\).*/cygwin/')

export	HOSTARCH HOSTOS

# Deal with colliding definitions from tcsh etc.
VENDOR=

#########################################################################

TOPDIR	:= $(shell if [ "$$PWD" != "" ]; then echo $$PWD; else pwd; fi)
export	TOPDIR

BUILDVERSION=$(shell if test -e $(KERNELPATH)/ath_version.mk ; then cat $(KERNELPATH)/ath_version.mk | sed s/EXTRAVERSION=-LSDK-//g; fi)

ifndef COMPRESSED_UBOOT
COMPRESSED_UBOOT = 0
endif 

ifeq (include/config.mk,$(wildcard include/config.mk))
# load ARCH, BOARD, and CPU configuration
include include/config.mk
export	ARCH CPU BOARD VENDOR SOC
ifndef CROSS_COMPILE
ifeq ($(ARCH),mips)
CROSS_COMPILE = mips-linux-
endif
endif

export	CROSS_COMPILE

# load other configuration
include $(TOPDIR)/config.mk

#########################################################################
# U-Boot objects....order is important (i.e. start must be first)
OBJS  = cpu/$(CPU)/start.o

ifeq ($(COMPRESSED_UBOOT),1)
OBJS_BOOTSTRAP  = cpu/$(CPU)/start_bootstrap.o
endif

LIBS  = lib_generic/libgeneric.a
LIBS += common/libcommon.a
LIBS += board/$(BOARDDIR)/lib$(BOARD).a
LIBS += cpu/$(CPU)/lib$(CPU).a
ifdef SOC
LIBS += cpu/$(CPU)/$(SOC)/lib$(SOC).a
endif
LIBS += lib_$(ARCH)/lib$(ARCH).a

LIBS += drivers/libdrivers.a
LIBS += disk/libdisk.a fs/fat/libfat.a fs/libfs.a
LIBS += net/libnet.a
LIBS += rtc/librtc.a
LIBS += $(BOARDLIBS)

ifeq ($(COMPRESSED_UBOOT),1)
LIBS_BOOTSTRAP = lib_bootstrap/libbootstrap.a 
#LIBS_BOOTSTRAP += lib_$(CPU)/lib$(CPU).a
LIBS_BOOTSTRAP += board/$(BOARDDIR)/lib$(BOARD).a 
LIBS_BOOTSTRAP += cpu/$(CPU)/lib$(CPU).a
ifneq ($(SOC),)
LIBS_BOOTSTRAP += cpu/$(CPU)/$(SOC)/lib$(SOC).a
endif
endif
.PHONY : $(LIBS)

ifeq ($(COMPRESSED_UBOOT),1)
.PHONY : $(LIBS_BOOTSTRAP)
endif

# Add GCC lib
PLATFORM_LIBS += -L $(shell dirname `$(CC) $(CFLAGS) -print-libgcc-file-name`) -lgcc


# The "tools" are needed early, so put this first
# Don't include stuff already done in $(LIBS)
SUBDIRS	= tools
.PHONY : $(SUBDIRS)

#########################################################################
#########################################################################

ALL = u-boot.srec u-boot.bin System.map

ifeq ($(COMPRESSED_UBOOT),1)
all:		$(ALL) tuboot.bin
else
all:		$(ALL)
endif

u-boot.hex:	u-boot
		$(OBJCOPY) ${OBJCFLAGS} -O ihex $< $@

u-boot.srec:	u-boot
		$(OBJCOPY) ${OBJCFLAGS} -O srec $< $@

u-boot.bin:	u-boot
		$(OBJCOPY) ${OBJCFLAGS} -O binary $< $@

u-boot.img:	u-boot.bin
		./tools/mkimage -A $(ARCH) -T firmware -C none \
		-a $(TEXT_BASE) -e 0 \
		-n $(shell sed -n -e 's/.*U_BOOT_VERSION//p' $(VERSION_FILE) | \
			sed -e 's/"[	 ]*$$/ for $(BOARD) board"/') \
		-d $< $@

u-boot.dis:	u-boot
		$(OBJDUMP) -d $< > $@

u-boot:		depend version $(SUBDIRS) $(OBJS) $(LIBS) $(LDSCRIPT)
		UNDEF_SYM=`$(OBJDUMP) -x $(LIBS) |sed  -n -e 's/.*\(__u_boot_cmd_.*\)/-u\1/p'|sort|uniq`;\
		$(LD) $(LDFLAGS) $$UNDEF_SYM $(OBJS) $(BOARD_EXTRA_OBJS) \
			--start-group $(LIBS) --end-group $(PLATFORM_LIBS) \
			-Map u-boot.map -o u-boot

$(LIBS):
		$(MAKE) -C `dirname $@`

$(SUBDIRS):
		$(MAKE) -C $@ all

ifeq ($(COMPRESSED_UBOOT),1)

LZMA = $(BUILD_DIR)/util/lzma

tuboot.bin:	System.map bootstrap.bin u-boot.lzimg	
		@cat bootstrap.bin > $@
		@cat u-boot.lzimg >> $@

u-boot.lzimg: $(obj)u-boot.bin System.map 
		@$(LZMA) e $(obj)u-boot.bin u-boot.bin.lzma
		@./tools/mkimage -A mips -T firmware -C lzma \
		-a 0x$(shell grep "T _start" $(TOPDIR)/System.map | awk '{ printf "%s", $$1 }') \
		-e 0x$(shell grep "T _start" $(TOPDIR)/System.map | awk '{ printf "%s", $$1 }') \
		-n 'u-boot image' -d $(obj)u-boot.bin.lzma $@

bootstrap.bin:	bootstrap
		$(OBJCOPY) ${OBJCFLAGS} -O binary $< $@

bootstrap:	depend version $(SUBDIRS) $(OBJS_BOOTSTRAP) $(LIBS_BOOTSTRAP) $(LDSCRIPT_BOOTSTRAP)
		UNDEF_SYM=`$(OBJDUMP) -x $(LIBS_BOOTSTRAP) |sed  -n -e 's/.*\(__u_boot_cmd_.*\)/-u\1/p'|sort|uniq`;\
		$(LD) $(LDFLAGS_BOOTSTRAP) $$UNDEF_SYM $(OBJS_BOOTSTRAP) \
			--start-group $(LIBS_BOOTSTRAP) --end-group $(PLATFORM_LIBS) \
			-Map bootstrap.map -o bootstrap

$(LIBS_BOOTSTRAP):
		$(MAKE) -C `dirname $@`
endif

version:
		@echo -n "#define U_BOOT_VERSION \"U-Boot " > $(VERSION_FILE); \
		echo -n "$(U_BOOT_VERSION)" >> $(VERSION_FILE); \
		echo -n $(shell $(CONFIG_SHELL) $(TOPDIR)/tools/setlocalversion \
			 $(TOPDIR)) >> $(VERSION_FILE); \
		echo "\"" >> $(VERSION_FILE)

gdbtools:
		$(MAKE) -C tools/gdb || exit 1

depend dep:
		@for dir in $(SUBDIRS) ; do $(MAKE) -C $$dir .depend ; done

tags:
		ctags -w `find $(SUBDIRS) include \
				lib_generic board/$(BOARDDIR) cpu/$(CPU) lib_$(ARCH) \
				fs/cramfs fs/fat fs/fdos fs/jffs2 \
				net disk rtc dtt drivers drivers/sk98lin common \
			\( -name CVS -prune \) -o \( -name '*.[ch]' -print \)`

etags:
		etags -a `find $(SUBDIRS) include \
				lib_generic board/$(BOARDDIR) cpu/$(CPU) lib_$(ARCH) \
				fs/cramfs fs/fat fs/fdos fs/jffs2 \
				net disk rtc dtt drivers drivers/sk98lin common \
			\( -name CVS -prune \) -o \( -name '*.[ch]' -print \)`

System.map:	u-boot
		@$(NM) $< | \
		grep -v '\(compiled\)\|\(\.o$$\)\|\( [aUw] \)\|\(\.\.ng$$\)\|\(LASH[RL]DI\)' | \
		sort > System.map

#########################################################################
else
all install u-boot u-boot.srec depend dep:
	@echo "System not configured - see README" >&2
	@ exit 1
endif

#########################################################################

unconfig:
	@rm -f include/config.h include/config.mk board/*/config.tmp


#========================================================================
# MIPS
#========================================================================

carambola2_config: unconfig hornet_common_config
	@echo "#define FLASH_SIZE $(FLASH_SIZE)" >>include/config.h
	@echo "#define CARABOOT_RELEASE \"$(CARABOOT_RELEASE)\"" >>include/config.h
ifeq ($(RAM_BOOT),1)
	@echo '#define CONFIG_SKIP_LOWLEVEL_INIT' >>include/config.h
endif
	@./mkconfig -a carambola2 mips mips carambola2 ar7240 ar7240

lima_config		:	unconfig
	@ >include/config.h
	@echo '#define CONFIG_ATHEROS		1'	>include/config.h
	@echo '#define CONFIG_MACH_QCA953x	1'	>>include/config.h
	@echo '#define SOC_TYPE QCA_QCA953X_SOC' 	>>include/config.h
	@echo '#define CFG_INIT_STACK_IN_SRAM	1'	>>include/config.h
	@echo '#define CONFIG_'`echo $(CFG_BOARD_TYPE) | tr [a-z] [A-Z] | sed s/-/_/g`'	1' >>include/config.h
	@echo '#define __CONFIG_BOARD_NAME $(CFG_BOARD_TYPE)' >>include/config.h
	@echo '#define CONFIG_BOARD_NAME "$(CFG_BOARD_TYPE)"' >>include/config.h
	@echo "#define CARABOOT_RELEASE \"$(CARABOOT_RELEASE)\"" >>include/config.h
	@echo '#define CFG_PLL_FREQ		CFG_PLL_650_600_200'	>>include/config.h
	@echo '#define CFG_ATHRS27_PHY 1' >>include/config.h
	@echo '#define CFG_ATH_GMAC_NMACS 2' >>include/config.h
	@echo '#define BOARD_NAME "$(CFG_BOARD_NAME)"' >>include/config.h
ifeq ($(RAM_BOOT),1)
	@echo '#define CONFIG_SKIP_LOWLEVEL_INIT' >>include/config.h
endif

ifdef FLASH_SIZE
	@echo "#define FLASH_SIZE $(FLASH_SIZE)" >>include/config.h
endif
	@./mkconfig -a board953x mips mips board953x atheros

hornet_common_config        :
	@ >include/config.h
	@echo "#define CONFIG_AR7240 1" >>include/config.h
	@echo "#define CONFIG_MACH_HORNET 1" >>include/config.h
ifeq ($(CONFIG_HORNET_XTAL), 40)
	@echo "#define CONFIG_40MHZ_XTAL_SUPPORT 1" >>include/config.h
endif
ifeq ($(CONFIG_HORNET_1_1_WAR), 1)
	@echo "#define CONFIG_HORNET_1_1_WAR 1" >>include/config.h
endif
ifeq ($(AG7240_BROADCAST_ENABLE), 1)
	@echo "#define AG7240_BROADCAST_ENABLE 1" >>include/config.h
endif
ifeq ($(NEW_DDR_TAP_CAL), 1)
	@echo "#define NEW_DDR_TAP_CAL 1" >>include/config.h
endif

board956x_config: unconfig
	@echo '#define CONFIG_ATHEROS		1'	>include/config.h
	@echo '#define CONFIG_MACH_QCA956x	1'	>>include/config.h
	@echo '#define SOC_TYPE QCA_QCA956X_SOC' 	>>include/config.h
	@echo '#define CFG_INIT_STACK_IN_SRAM	1'	>>include/config.h
	@echo '#define CONFIG_'`echo $(CFG_BOARD_TYPE) | tr [a-z] [A-Z] | sed s/-/_/g`'	1' >>include/config.h
	@echo '#define __CONFIG_BOARD_NAME $(CFG_BOARD_TYPE)' >>include/config.h
	@echo '#define CONFIG_BOARD_NAME "$(CFG_BOARD_TYPE)"' >>include/config.h
	@echo '#define CFG_PLL_FREQ		CFG_PLL_775_650_258'	>>include/config.h

ifdef ddr_cas
	@echo '#define CFG_DDR2_DRAGONFLY_CAS_LATENCY		$(ddr_cas)'	>>include/config.h
endif
ifneq (,$(findstring $(CFG_BOARD_TYPE), ap152 tb755)) #{
	@echo '#define CFG_ATH_GMAC_NMACS	1'	>>include/config.h
	@echo '#define CFG_ATH_GE1_IS_CONNECTED 1' 	>>include/config.h
	@echo '#define CONFIG_ATHRS_GMAC_SGMII  1'      >>include/config.h
	@echo '#define ATH_S17_MAC0_SGMII	1'	>>include/config.h
endif #}

ifneq (,$(findstring $(CFG_BOARD_TYPE), ap152)) #{
	@echo '#define UART_RX18_TX22           1'      >>include/config.h
endif #}

ifneq (,$(findstring $(CFG_BOARD_TYPE), tb753 tb754)) #{
	@echo '#define TEST_BOARD_UART          1'      >>include/config.h
endif #}

ifneq (,$(findstring $(CFG_BOARD_TYPE),dragonflyemu)) #{
	@echo '#define CFG_ATH_GMAC_NMACS	2'	>>include/config.h
	@echo '#define CFG_ATH_GE1_IS_CONNECTED 1' 	>>include/config.h
	@echo '#define CONFIG_ATHRS_GMAC_SGMII  1'      >>include/config.h
endif #}

ifeq ($(CFG_BOARD_TYPE), ap152)
	@echo '#define CONFIG_ATHRS_GMAC_SGMII	1'	>>include/config.h
endif

ifeq ($(ETH_CONFIG), _s27)
	@echo '#define CFG_ATHRS27_PHY		1'	>>include/config.h	
endif

ifeq ($(ETH_CONFIG), _s17)
	@echo '#define CONFIG_ATHRS17_PHY	1'	>>include/config.h
endif

ifeq ($(ATH_SGMII_FORCED),1)
	@echo '#define ATH_SGMII_FORCED_MODE    1'	>>include/config.h
endif

ifdef FLASH_SIZE
	@echo '#define FLASH_SIZE	$(FLASH_SIZE)'	>>include/config.h
endif
	@./mkconfig -a board956x mips mips board956x atheros

###############

clean:
	@echo Making $@
	@find . -type f \
		\( -name 'core' -o -name '*.bak' -o -name '*~' \
		-o -name '*.o'  -o -name '*.a' -o -name .depend \) -print \
		| xargs rm -f
	@rm -f examples/hello_world examples/timer \
	      examples/eepro100_eeprom examples/sched \
	      examples/mem_to_mem_idma2intr examples/82559_eeprom \
	      examples/smc91111_eeprom \
	      examples/test_burst
	@rm -f tools/img2srec tools/mkimage tools/envcrc tools/gen_eth_addr
	@rm -f tools/mpc86x_clk tools/ncb
	@rm -f tools/easylogo/easylogo tools/bmp_logo
	@rm -f tools/gdb/astest tools/gdb/gdbcont tools/gdb/gdbsend
	@rm -f tools/env/fw_printenv tools/env/fw_setenv
	@rm -f board/cray/L1/bootscript.c board/cray/L1/bootscript.image
	@rm -f board/netstar/eeprom board/netstar/crcek
	@rm -f board/netstar/*.srec board/netstar/*.bin
	@rm -f board/trab/trab_fkt board/voiceblue/eeprom
	@rm -f board/integratorap/u-boot.lds board/integratorcp/u-boot.lds
ifeq ($(COMPRESSED_UBOOT),1)
	@rm -f lib_bootstrap/*.o
	@rm -f lib_bootstrap/*.a
	@rm -f bootstrap bootstrap.bin tuboot.bin u-boot.lzimg
endif

clobber:	clean
	@echo Making $@
	@find . -type f \( -name .depend \
		-o -name '*.srec' -o -name '*.bin' -o -name u-boot.img \) \
		-print0 \
		| xargs -0 rm -f
	@rm -f $(OBJS) *.bak tags TAGS include/version_autogenerated.h
	@rm -fr *.*~
	@rm -f u-boot u-boot.map u-boot.hex $(ALL)
	@rm -f tools/crc32.c tools/environment.c tools/env/crc32.c
	@rm -f tools/inca-swap-bytes cpu/mpc824x/bedbug_603e.c
	@rm -f include/asm/proc include/asm/arch include/asm

mrproper \
distclean:	clobber unconfig

backup:
	F=`basename $(TOPDIR)` ; cd .. ; \
	gtar --force-local -zcvf `date "+$$F-%Y-%m-%d-%T.tar.gz"` $$F

#########################################################################
